package com.jaydenxiao.common.base;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

/**
 * Created by hasee on 2017/10/7.
 */

public abstract class BaseDialogFragment<V extends ViewDataBinding> extends DialogFragment {

    protected V viewDataBinding;
    protected BaseActivity mActivity;

    protected abstract int getLayoutId();
    protected abstract void initView();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        viewDataBinding=DataBindingUtil.inflate(inflater,getLayoutId(),container,false);
        initView();
        return viewDataBinding.getRoot();

    }




    public void removeFragment(){
        if (mActivity.getSupportFragmentManager().getBackStackEntryCount()>=1){
            mActivity.getSupportFragmentManager().popBackStack();
        }else {
            mActivity.finish();
        }
    }


    public void addFragment(BaseDialogFragment from, BaseDialogFragment to){
        mActivity.addFragment(from,to);

    }


    public void switchFragment(BaseDialogFragment to){
        mActivity.switchFragment(to);
    }


    public void backToSpecifiedFragment(String fragmentName){
        int fragmentCount=mActivity.getSupportFragmentManager().getBackStackEntryCount();
        for (int i=0;i<fragmentCount;i++){
            FragmentManager.BackStackEntry entry=mActivity.getSupportFragmentManager().getBackStackEntryAt(i);
            if (entry.getName().equals(fragmentName)){
                break;
            }
            mActivity.getSupportFragmentManager().popBackStackImmediate();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        initView();
//        getDialog().setTitle(getTitle());
    }


    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels - (int) (dm.widthPixels * 0.1f),
                getDialog().getWindow().getAttributes().height);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity= (BaseActivity) context;
    }


    @Override
    public void onDestroyView() {
        if (viewDataBinding!=null){
            viewDataBinding.unbind();
        }
        super.onDestroyView();
    }

    protected abstract String getTitle();

}
